// /*
//  * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
//  * Licensed under the Apache License, Version 2.0 (the "License");
//  * you may not use this file except in compliance with the License.
//  * You may obtain a copy of the License at
//  *
//  *     http://www.apache.org/licenses/LICENSE-2.0
//  *
//  * Unless required by applicable law or agreed to in writing, software
//  * distributed under the License is distributed on an "AS IS" BASIS,
//  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  * See the License for the specific language governing permissions and
//  * limitations under the License.
//  */

#include <stdio.h>
#include "los_task.h"
#include "ohos_init.h"
#include "file1.h" // 根据您的实际包含路径调整

// 定义PWM设备的类型和初始设置
enum EnumPwmDev {
    EPWMDEV_PWM0_M1 = 0,
};
static int pwm_initialized = 0; // 用于跟踪PWM是否已经初始化
unsigned int port = EPWMDEV_PWM0_M1; // PWM 端口
unsigned short duty = 10;            // 初始占空比设置为 10%
unsigned int freq = 50;              // PWM 频率设置为 50Hz

// 初始化 PWM
void pwm_init_once() {
    unsigned int ret = IoTPwmInit(port);
    if (ret != 0) {
        printf("IoTPwmInit failed(%d)\n", ret);
    }
}

// 反初始化 PWM
void pwm_deinit_once() {
    int ret = IoTPwmDeinit(port);
    if (ret != 0) {
        printf("IoTPwmDeinit failed(%d)\n", ret);
    }
}

// 启动 PWM
int pwm_start(unsigned int port, unsigned short duty, unsigned int freq) {
    int ret = IoTPwmStart(port, duty, freq);
    if (ret != 0) {
        printf("IoTPwmStart failed(%d)\n", ret);
        IoTPwmDeinit(port);
    }
    return ret;
}

// 停止 PWM
void pwm_stop(unsigned int port) {
    int ret = IoTPwmStop(port);
    if (ret != 0) {
        printf("IoTPwmStop failed(%d)\n", ret);
    }
}

// 改变 PWM 占空比
void pwm_change_duty(unsigned int *duty, int direction) {
    *duty += direction * 10;
    if (*duty < 1) {
        *duty = 1;
    } else if (*duty > 99) {
        *duty = 99;
    }
}

// 反初始化 PWM 并重置初始化标志
void pwm_deinit_and_reset() {
    if (pwm_initialized) {
        IoTPwmDeinit();
        pwm_initialized = 0; // 重置初始化标志
        printf("PWM has been deinitialized.\n");
    }
}


// 控制 PWM 来回转动
void hal_pwm_swing_control(SWITCH_STATUS_ENUM status) {
    unsigned int on_duty = 20; // 示例：占空比设置为 20% 表示 ON 状态
    unsigned int off_duty = 5; // 示例：占空比设置为 5% 表示 OFF 状态

    // 初始化 PWM（如果尚未初始化）
    if (!pwm_initialized) {
        if (IoTPwmInit() != 0) return;
        pwm_initialized = 1; // 标记为已初始化
    }

    // 根据状态设置占空比
    unsigned int current_duty = (status == ON) ? on_duty : off_duty;

    // 启动 PWM
    if (pwm_start(port, current_duty, freq) != 0) {
        return;
    }

    // 维持当前状态一段时间
    LOS_Msleep(2000);

    // 停止 PWM
    pwm_stop(port);

    // 在这里不再直接调用 IoTPwmDeinit
    // 而是调用 pwm_deinit_and_reset 来反初始化 PWM，并重置标志
    pwm_deinit_and_reset();
}
void hal_pwm_on_control() {
    // 初始化 PWM
    if (IoTPwmInit() != 0) {
        printf("PWM Init failed\n");
        return;
    }

    // 启动 PWM 使用全局的 duty 变量
    if (pwm_start(port, duty, freq) != 0) {
        printf("PWM Start failed\n");
        IoTPwmDeinit();
        return;
    }

    printf("PWM ON with duty cycle: %u%%\n", duty);

    // 维持当前状态一段时间
    LOS_Msleep(2000);

    // 停止 PWM
    pwm_stop(port);

    // 反初始化 PWM
    IoTPwmDeinit();
}